//
//  HEMyOrderTabbarView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMyOrderTabbarView.h"


static CGFloat margin = 10.f;
#define ButtonW 80

@interface HEMyOrderTabbarView ()

@property (nonatomic, strong) NSMutableArray *ButtonArray;
@property (nonatomic, strong) NSMutableArray *itemsArr;
@property (nonatomic, strong) NSArray *btnModels;

@end

@implementation HEMyOrderTabbarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        
        self.qmui_borderPosition = QMUIViewBorderPositionTop;
        self.qmui_borderWidth = 1;
        self.qmui_borderColor = CSplitLineColor;
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    for (int i = 0; i < 3; i++) {
        
        NSInteger row1 = i/3;
        NSInteger colum = i%3;
        
        QMUIGhostButton *checkBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
        checkBtn.titleLabel.font = DYBlodFont(14);
        [checkBtn setTitle:@"查看订单" forState:UIControlStateNormal];
        checkBtn.frame = CGRectMake((margin + ButtonW)*colum, (margin + ButtonW)*row1, ButtonW, 28);
        checkBtn.hidden = YES;
        checkBtn.tag = i;
        [self addSubview:checkBtn];
        
        [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            if (i == 0) {
                make.right.equalTo(self).mas_offset(-15);
            }else {
                QMUIGhostButton *latBtn = self.ButtonArray[i-1];
                make.right.equalTo(latBtn.mas_left).mas_offset(-margin);
            }
            make.height.mas_equalTo(28);
            make.width.mas_equalTo(ButtonW);
        }];
        [self.ButtonArray addObject:checkBtn];
    }
}

- (void)setModel:(HEOrderModel *)model {
    
    _model = model;
    
    _btnModels = @[];
    
    // 居家照料  护工订单
    if (model.channel == HEChannelTypeHomeCare && model.status != HEOrderStatusWaitPay) {
        switch (model.welfareStatus) {
            case HEOrderWelfareStatusEvaluated: // 待评价
            {
                _btnModels = @[@{@"name":@"评价", @"color":CPriceColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeComment]}];
            }
                break;
            case HEOrderWelfareStatusCanceled:
            case HEOrderWelfareStatusAutoRefurn:
            {
                _btnModels = @[@{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]}];
            }
                break;
            default:
                _btnModels = @[];
                break;
        }
     
    }else {
        switch (model.status) {
            case HEOrderStatusWaitPay:
            {
                _btnModels = @[@{@"name":@"付 款", @"color":CPriceColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeToPay]},
                         @{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]},
                         @{@"name":@"取消订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCancelOrder]},];
            }
                break;
            case HEOrderStatusWaitDeliver:
            {
   
            }
                break;
            case HEOrderStatusDelivered:
            {
                _btnModels = @[@{@"name":@"确认收货", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeConfirmGoods]},
                         @{@"name":@"查看物流", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeChecklogistics]}];
            }
                break;
            case HEOrderStatusWaitComment:
            {
                _btnModels = @[@{@"name":@"评价", @"color":CPriceColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeComment]}];
            }
                break;
            case HEOrderStatusFinish:
            case HEOrderStatusClosed:
            {
                _btnModels = @[
                         @{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]}];
            }
                break;
            default:
                _btnModels = @[];
                break;
        }
    }
    
    if ([self showBatchAfter]) {
        NSMutableArray *arr = _btnModels.mutableCopy;
        [arr addObject:@{@"name":@"批量售后", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeBatchApplysales]}];
        _btnModels = arr;
    }
    
    [self.ButtonArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull button, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < self.btnModels.count) {
            button.hidden = NO;
            NSDictionary *dict = self.btnModels[idx];
            QMUIGhostButton *latBtn = self.ButtonArray[idx];
            [latBtn setTitle:dict[@"name"] forState:UIControlStateNormal];
            [latBtn setTitleColor:dict[@"color"] forState:UIControlStateNormal];
            latBtn.ghostColor = dict[@"color"];
            [latBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            button.hidden = YES;
        }
    }];
}

- (BOOL)showBatchAfter {
    BOOL after = YES;
    
    for (HEOrderItemModel *model in self.model.orderItemList) {
        if (model.afterOperationStatus != HEOrderGoodsOperationTypeApplySales) {
            after = NO;
            break;
        }
    }
    
    return after;
}

- (void)btnClick:(QMUIGhostButton *)btn {
    NSInteger tag = btn.tag;
    NSDictionary *dict = self.btnModels[tag];
    NSNumber *type = dict[@"type"];
    
    if ([_delegate respondsToSelector:@selector(bottomOperationWithModel:type:)]) {
        [_delegate bottomOperationWithModel:_model type:type.integerValue];
    }
        
    if (self.oprationBlock) {
        self.oprationBlock(type.integerValue, _model);
    }
}

#pragma mark - Getter

- (NSMutableArray *)ButtonArray {
    if(!_ButtonArray){
        _ButtonArray = [[NSMutableArray alloc] init];
    }
    return _ButtonArray;
}

@end
